package server.main.utils.mainutils;

import java.io.*;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
//import org.json.JSONException;
//import org.json.JSONObject;

/************
 *
 *@author : MingHs
 *@create : 2022/6/2
 *****************
 */
public class StopProject {

//    public static void restart(String path) {
//
//        BufferedReader br = null;
//        //
//        String lines = "";
//        try {
//
//            Process p = Runtime.getRuntime().exec("lsof -i:8080");
////            System.out.println(p);
//            br = new BufferedReader(new InputStreamReader(p.getInputStream()));
//            System.out.println("***********Getting PID*************");
//            br.readLine();
//            lines = br.readLine();
//            lines = lines.replaceAll(" {2,}", " ");
//            String Pid = lines.split(" ")[1];
//            System.out.println("************Success for Getting PID,PID:" + Pid + "***************");
//            System.out.println("************ready to kill Project************");
//            Process killpid = Runtime.getRuntime().exec("kill -9 " + Pid);
//            br = new BufferedReader(new InputStreamReader(killpid.getInputStream()));
//            while ((lines = br.readLine()) != null) {
//                System.out.println(lines);
//            }
//            System.out.println("************kill project successfully************");
//            //start the new project
//            URL resource = bianyitest.class.getClassLoader().getResource("");
//            System.out.println(resource);
//            System.out.println("************Getting the new Project************");
//            //get num of version
////            String version = "";
////            String version = Getjsonparam("/home/minghs/important/code/blood/BloodTransfusionManagement/restart/confi.json", "version");
//            String version = Getjsonparam("/home/minghs/important/code/blood/BloodTransfusionManagement/restart/confi.txt", "").get("version");
//
//            String newversionname = "BloodTransfusionManagement-" + version + ".jar";
//            System.out.println(newversionname);
//            Process startlog = Runtime.getRuntime().exec("nohup java -jar " + newversionname);
//            br = new BufferedReader(new InputStreamReader(startlog.getInputStream()));
//            while ((lines = br.readLine()) != null)
//                System.out.println(lines);
//
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            if (br != null) {
//                try {
//                    br.close();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//
//
//    }

    public static String getPid(String port) {
        BufferedReader br = null;
        String lines = null;
        String Pid = null;
        try {
            Process p = Runtime.getRuntime().exec("lsof -i:" + port);
//            System.out.println(p);
            br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            System.out.println("***********Getting PID*************");
            lines = br.readLine();
            if (lines == null)
                System.out.printf(port + "is free");
            else {
                lines = br.readLine();
                lines = lines.replaceAll(" {2,}", " ");
                Pid = lines.split(" ")[1];
                System.out.println("************Success for Getting PID,PID:" + Pid + "***************");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return Pid;
    }

    public static void restatt() {
        Stop("8080");
        start();
    }

    public static void Stop(String port) {

        BufferedReader br = null;
        //
        String lines = "";
        try {
            String Pid = null;
            while ((Pid = getPid(port)) != null) {
                System.out.println("************ready to kill Project************");
                Process killpid = Runtime.getRuntime().exec("kill -9 " + Pid);
                br = new BufferedReader(new InputStreamReader(killpid.getInputStream()));
                while ((lines = br.readLine()) != null) {
                    System.out.println(lines);
                }
                System.out.println("************kill project successfully************");
            }
            System.out.println("*********sttop finish****************");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }


    }

    public static void start() {
        BufferedReader br = null;
        //
        String lines = "";
        try {
            //start the new project
            URL resource = StopProject.class.getClassLoader().getResource("");
            System.out.println(resource);
            System.out.println("************Getting the new Project************");
            //get num of version
//            String version = "";
            String version = Getjsonparam("/home/minghs/important/code/blood/BloodTransfusionManagement/restart/confi.txt", "").get("version");
            String newversionname = "BloodTransfusionManagement-" + version + ".jar";
            System.out.println(newversionname);
            Process startlog = Runtime.getRuntime().exec("nohup java -jar " + "/home/minghs/important/code/blood/BloodTransfusionManagement/" + newversionname);
            br = new BufferedReader(new InputStreamReader(startlog.getInputStream()));
            while ((lines = br.readLine()) != null)
                System.out.println(lines);


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static String readFile(String filename) {
        try {
            InputStream is = new FileInputStream(filename);
            int iAvail = is.available();
            byte[] bytes = new byte[iAvail];
            is.read(bytes);
            String str = new String(bytes);
//            System.out.println(str);
//            logger.info("文件内容:\n" + new String(bytes));
            is.close();
            return str;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static Map<String, String> Getjsonparam(String path, String param) {
        Map<String, String> map = new HashMap<>();
        String strtemp = "";
        try {
            String lines;
            File file = new File(path);
            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
            while ((lines = bufferedReader.readLine()) != null) {
//                System.out.println(lines);
                String[] split = lines.split("=");
                map.put(split[0], split[1]);
            }


        } catch (Exception e) {
            e.printStackTrace();

        }

//        List<String> goal = new ArrayList<>();

//        String mainfilepath = readFile(path);
//        JSONObject myJson = null;
//        try {
//            myJson = new JSONObject(mainfilepath);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//        return  myJson.optString(param);
        return map;
    }

    public static void main(String[] args) {
        StopProject.Stop("8080");
    }


}
